本文介绍了如果选择了单选按钮,则显示字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在上获取网站网址字段,以便仅在上一个问题选中单选按钮是。我搜索并尝试了一些代码示例,但它们无法正常工作。有没有人对我有任何建议?

I'm attempting to get the Website URL field on this page to display only when the previous question has the radio button "Yes" selected. I've searched and tried a few code examples, but they aren't working. Does anyone have any suggestions for me?

提前致谢!

<div class="editfield">
<div class="radio">
    <span class="label">Do you have your own website? (required)</span>
    <div id="field_8"><label><input type="radio" name="field_8" id="option_9" value="Yes"> Yes</label><label><input type="radio" name="field_8" id="option_10" value="No"> No</label></div>
</div>
</div>

<div class="editfield">
<label for="field_19">Website URL </label>
<input type="text" name="field_19" id="field_19" value="" />
</div>


推荐答案

我注意到你最初把我给你的javascript在页面顶部。如果要执行此操作,则需要将代码封装在jquery $(document).ready(function(){}); 中。当你的html跟在javascript之后,你只需要准备一份文件。

I noticed that you initally put the javascript I gave you at the top of the page. If you are going to do this then you need to encapsulate the code in a jquery $(document).ready(function(){ });. You only need to use a document ready when your html follows after the javascript.

$(function() {
    // place code here
});

然而,在这种情况下,我创造了另一种更好的选择,但不要忘记你必须首先将web url div设置为隐藏。另外,我强烈建议您设置更好的控件ID;它会让你的javascript更容易理解。

However, in this scenario I have created another alternative that will be better, but do not forget that you have to initially set the web url div as hidden. Also, I highly recommend that you set better control ids; it will make your javascript easier to understand.

$('input[name=field_8]').on("click",  function(){
    var $div_WebUrl = $('#field_19').closest('.editfield');

    if($('input[name=field_8]').index(this) == 0)
        $div_WebUrl.show();
    else
        $div_WebUrl.hide();
});​

这篇关于如果选择了单选按钮,则显示字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:23