1

这是在 jQuery 中传递两个 id 的正确方法吗?

如图所示,我遇到了以下问题。

域注册:不显示输入框。

域名转移:只显示一个输入框。

图片:

在此处输入图像描述

CSS:

#domainToBeReged, #domainToBeReged0, #domainToBeTransfered{
    display:none;
}

HTML:

<label for="domainRequired">Domain Registration: </label>
                    <select name="domainRequired" id="domainRequired">
                        <option value="pleaseSelect">Please Select</option>
                        <option value="yes">Yes</option>
                        <option value="no">No</option>
                    </select>
        <div id="domainToBeReged">
            <label for="domainToBeReged">Domain One:</label><input name="domainToBeReged" id="domainToBeReged" type="text" placeholder="http://www." />
            <label for="domainToBeReged0">Domain Two:</label><input name="domainToBeReged0" id="domainToBeReged0" type="text" placeholder="http://www." />

        </div>
        <label for="domainTransfer">Domain Transfer: </label>
                    <select name="domainTransfer" id="domainTransfer">
                        <option value="pleaseSelect">Please Select</option>
                        <option value="yes">Yes</option>
                        <option value="no">No</option>
                    </select>
        <div id="domainToBeTransfered">
                    <label for="domainToBeTransfered">Domain:</label><input name="domainToBeTransfered" id="domainToBeTransfered" type="text" placeholder="http://www." />
                  <label for="domainToBeTransfered0">Domain:</label><input name="domainToBeTransfered0" id="domainToBeTransfered0" type="text" placeholder="http://www." />
        </div>
        <label for="currentHosting">Current Hosting: </label>
        <textarea cols="10" rows="10" name="currentHosting" id="currentHosting"></textarea>
        <input type="submit" class="nextButton" value="Next" />
        </fieldset>
    </form>

jQuery:

    jQuery(document).ready(function() { 
$('select[name="domainRequired"]').change(function() {
            var $domain = $('#domainToBeReged','#domainToBeReged0');
                if ($(this).val() == 'yes') {
                    $domain.show();
                } else {
                    $domain.hide(); 
        }
            });             


        $('select[name="domainTransfer"]').change(function() {
            var $domain = $('#domainToBeTransfered');
                if ($(this).val() == 'yes') {
                    $domain.show();
                } else {
                    $domain.hide(); 
        }
            });             

    });
4

2 回答 2

4

这并不完全正确:

  • $("selector1", "selector2")正在寻找selector1里面selector2。它基本上相当于$("selector2").find("selector1"). 您似乎只需要两个独立的选择器。请改用逗号选择器
  • 您可以使用和/.toggle(bool)来消除。ifshowhide
$("#domainToBeReged, #domainToBeReged0").toggle($(this).val() === "yes");
于 2012-04-06T21:30:03.737 回答
1

不,您的 jquery 必须是:

$('#domainToBeReged,#domainToBeReged0');
于 2012-04-06T21:29:55.407 回答