如何简化我的jQuery代码以避免重复指令?

huangapple go评论70阅读模式
英文:

How can I simplify my jQuery code to avoid repeating instructions?

问题

I am a novice with Javascript and jQuery, but have written some code to display a tooltip on a form depending on the answer selected to a dropdown. Right now I am repeating the steps twice:

  1. Once to check the dropdown on page load, in case it has reloaded due to a submission error - in this case, if an answer has been selected to that question, it will persist, and I need the tooltip to remain.

  2. Once to check whenever the dropdown value changes.

The value of the dropdown is a number, so I've used that in the div classes to show the appropriate div. This is the code which is working fine:

$(document).ready(function(){
    var service = "";
    var otherservice = "";

    // Check for select value on page load, in case it has refreshed due to a form error

    service = '.v' + $('select#989022_58716pi_989022_58716 option:selected').val();
    otherservice = '#form-tooltips div:not('+service+')';
    $('#form-tooltips div'+service).show();
    $(otherservice).hide();

    // Check again for select value, every time that selection changes

    $('select#989022_58716pi_989022_58716').on('change', function(){
    	service = '.v' + $('select#989022_58716pi_989022_58716 option:selected').val();
        otherservice = '#form-tooltips div:not('+service+')';
       	$('#form-tooltips div'+service).show();
        $(otherservice).hide();
    });
});

//The tooltips for display

$("<div id='form-tooltips'><div class='v1381962'>Tooltip 1</div><div class='v1381965'>Tooltip 2</div></div>").insertAfter(".add-tooltip-after");

What I would like to do is create a function - checkTooltip - so that I do not have to repeat those tooltip instructions the second time. I have tried the following:

$(document).ready(function(){
    var service = '';
    var otherservice = '';
    function checkTooltip() {
        service = '.v' + $('select#989022_58716pi_989022_58716 option:selected').val();
        otherservice = '#form-tooltips div:not('+service+')';
       	$('#form-tooltips div'+service).show();
        $(otherservice).hide();
    }
    checkTooltip();
    $('select#989022_58716pi_989022_58716').on('change', checkTooltip);
});
$("<div id='form-tooltips'><div class='v1381962'>Tooltip 1</div><div class='v1381965'>Tooltip 2</div></div>").insertAfter(".add-tooltip-after");

However this is not working. In the Chrome console, it says Uncaught SyntaxError: Unexpected token ';' on the 5th line. I have tried removing that semicolon but then it gives me Unexpected identifier 'otherservice' instead.

Am I completely misunderstanding how this works or making some kind of syntax error? Many thanks in advance to anyone who can help!

英文:

Novice trying to simplify my jQuery, to avoid repetition

I am a novice with Javascript and jQuery, but have written some code to display a tooltip on a form depending on the answer selected to a dropdown. Right now I am repeating the steps twice:

  1. Once to check the dropdown on page load, in case it has reloaded due to a submission error - in this case, if an answer has been selected to that question, it will persist, and I need the tooltip to remain.

  2. Once to check whenever the dropdown value changes.

The value of the dropdown is a number, so I've used that in the div classes to show the appropriate div. This is the code which is working fine:

$(document).ready(function(){
    var service = &quot;&quot;;
    var otherservice = &quot;&quot;;

    // Check for select value on page load, in case it has refreshed due to a form error

    service = &#39;.v&#39; + $(&#39;select#989022_58716pi_989022_58716 option:selected&#39;).val();
    otherservice = &#39;#form-tooltips div:not(&#39;+service+&#39;)&#39;;
    $(&#39;#form-tooltips div&#39;+service).show();
    $(otherservice).hide();

    // Check again for select value, every time that selection changes

    $(&#39;select#989022_58716pi_989022_58716&#39;).on(&#39;change&#39;, function(){
    	service = &#39;.v&#39; + $(&#39;select#989022_58716pi_989022_58716 option:selected&#39;).val();
        otherservice = &#39;#form-tooltips div:not(&#39;+service+&#39;)&#39;;
       	$(&#39;#form-tooltips div&#39;+service).show();
        $(otherservice).hide();
    });
});

//The tooltips for display

$(&quot;&lt;div id=&#39;form-tooltips&#39;&gt;&lt;div class=&#39;v1381962&#39;&gt;Tooltip 1&lt;/div&gt;&lt;div class=&#39;v1381965&#39;&gt;Tooltip 2&lt;/div&gt;&lt;/div&gt;&quot;).insertAfter(&quot;.add-tooltip-after&quot;);

What I would like to do is create a function - checkTooltip - so that I do not have to repeat those tooltip instructions the second time. I have tried the following:

$(document).ready(function(){
    var service = &#39;&#39;;
    var otherservice = &#39;&#39;;
    function checkTooltip({
        service = &#39;.v&#39; + $(&#39;select#989022_58716pi_989022_58716 option:selected&#39;).val();
        otherservice = &#39;#form-tooltips div:not(&quot;+service+&quot;)&#39;;
       	$(&#39;#form-tooltips div&#39;+service).show();
        $(otherservice).hide();
    });
    checkTooltip();
    $(&#39;select#989022_58716pi_989022_58716&#39;).on(&#39;change&#39;, checkTooltip());
});
$(&quot;&lt;div id=&#39;form-tooltips&#39;&gt;&lt;div class=&#39;v1381962&#39;&gt;Tooltip 1&lt;/div&gt;&lt;div class=&#39;v1381965&#39;&gt;Tooltip 2&lt;/div&gt;&lt;/div&gt;&quot;).insertAfter(&quot;.add-tooltip-after&quot;);

However this is not working. In the Chrome console, it says Uncaught SyntaxError: Unexpected token ';' on the 5th line. I have tried removing that semicolon but then it gives me Unexpected identifier 'otherservice' instead.

Am I completely misunderstanding how this works or making some kind of syntax error? Many thanks in advance to anyone who can help!

答案1

得分: 0

代码中的语法明显有问题,但问题出在函数定义部分。应该是这样的。不能保证功能是否正确:

$(document).ready(function(){
    let service = '';
    let otherservice = '';
    const checkTooltip = () => {
        service = '.v' + $('select#989022_58716pi_989022_58716 option:selected').val();
        otherservice = '#form-tooltips div:not("'+service+'")';
        $('#form-tooltips div'+service).show();
        $(otherservice).hide();
    }
    checkTooltip();
    $('select#989022_58716pi_989022_58716').on('change', checkTooltip);
});

还要注意最后的on.change处理程序的更改。

此外,虽然超出了你的问题范围,但989022_58716pi_989022_58716 可能可以使用更具人可读性的 ID 或类名进行重写。

英文:

The syntax is definitely wrong, but in the function definition. It should look like this. No guarantees on whether the functionality is correct.:

$(document).ready(function(){
    let service = &#39;&#39;;
    let otherservice = &#39;&#39;;
    const checkTooltip = () =&gt; {
        service = &#39;.v&#39; + $(&#39;select#989022_58716pi_989022_58716 option:selected&#39;).val();
        otherservice = &#39;#form-tooltips div:not(&quot;+service+&quot;)&#39;;
        $(&#39;#form-tooltips div&#39;+service).show();
        $(otherservice).hide();
    }
    checkTooltip();
    $(&#39;select#989022_58716pi_989022_58716&#39;).on(&#39;change&#39;, checkTooltip);
});

Note the change in the on.change handler at the end too.

Also its out of scope for your question but 989022_58716pi_989022_58716 probably could be rewritten with a more human-readable id or class.

huangapple
  • 本文由 发表于 2023年6月1日 23:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定