简体   繁体   English

jQuery单选按钮更改问题

[英]jquery radiobutton change issue

I have 2 radiobuttons and I make some fields active/inactive via radiobuttons. 我有2个单选按钮,我通过单选按钮使某些字段处于活动/非活动状态。 but when I send the data from the form via ajax and then open my form for the second time without reloading the page, the radio buttons are broken, ie they don't provide active/inactive functionality. 但是,当我通过Ajax从表单发送数据,然后第二次打开表单而不重新加载页面时,单选按钮已损坏,即它们不提供活动/非活动功能。 So, for instance, when I click button 2, field3 becomes active, i put needed data and send it to server via ajax, but when I open my dialog for the second time, radiobutton 2 is still active and when I try to change ot to radiobutton 1 fields 1,2 are still inactive, but when I reload the page, everything is ok. 因此,例如,当我单击按钮2时,field3处于活动状态,我放置了所需的数据并通过ajax将其发送到服务器,但是当我第二次打开对话框时,单选按钮2仍然处于活动状态,并且当我尝试更改ot时到单选按钮1的字段1,2仍然无效,但是当我重新加载页面时,一切正常。 what's the matter? 怎么了? here is my html code: 这是我的html代码:

<a href="javascript:void(0)" class="add">click here</a>

<div id="dialog-form" title="title">
    <p class="validateTips">All form fields matched with (*) are required</p>
    <form>
        <fieldset>
            <div>
                <div>
                    <label for="name">Name(*)</label>
                    <textarea id="myName" class="text ui-widget-content ui-corner-all"></textarea>
                </div>
            </div>
            <div>
                <label for="perc">percent(*)</label>
                <input type="number" id ="perc"  step="0.5" />
            </div>
            <div> 
            <table>
                <tr>
                <td>
                    <input type="radio" id="but1" name="group1" value="1">1<br>
                </td>
                <td>
                    <input type="radio" id="but2" name="group1" value="2">2<br>
                </td>
                </tr>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <label for="field1">field1 (*)</label>    
                                <input type="text" id="field1" value="" class="group1" > <br>    
                            </tr>
                            <tr>
                                <label for="field2">field2 (*)</label>    
                                <input type="text" id="field2" value="" class="group1" > <br>    
                            </tr>

                        </table>
                    </td>
                    <td>
                            <label for="field3">field3 (*)</label>
                            <input id="field3" type="text" value="" class="group2" >
                    </td>
                </tr>
            </table>
           </div>


        </fieldset>
    </form>
</div>

and here is jQuery code, that changes activity of fields via radiobuttons: 这是jQuery代码,可通过单选按钮更改字段的活动:

$(document).ready(function() {
    $("input:radio").on("click", function() {
            makeFieldsActive($(this));
        }); 
});

function makeFieldsActive(elem) {

    $("input:text").prop("disabled", true);
     $("input.group" + elem.val()).prop("disabled", false);
}

Two possibilities here (at least) : 这里有两种可能性(至少):

You could empty the content of your dialog and append it again, you would be sure everything is set by default. 您可以清空对话框的内容,然后再次附加它,您可以确保所有设置都是默认设置。 The easiest to try it out is to create new html page which content would be your form (no need for the usual html tags), and load it into your div. 最容易尝试的方法是创建一个新的html页面,该页面内容就是您的表单(不需要通常的html标签),并将其加载到div中。 It would be the same as reloading your page. 这与重新加载页面相同。 But also be careful, some browsers tend to keep the forms data when you reload a page so it might not be the solution you actually need : 但也要小心,某些浏览器在重新加载页面时倾向于保留表单数据,因此它可能并不是您真正需要的解决方案:

$("#dialog-form").empty().load("form.html");

Other way is to use a function that will reset all your inputs to default : 其他方法是使用将所有输入重置为默认值的功能:

$("input").prop("disabled",false);

So where would you put this code? 那么您会将代码放在哪里? I'd suggest to do it when you open your dialog, there is an event especially for that purpose called dialogopen : 我建议您在打开对话框时执行此操作,有一个专门用于此目的的事件名为dialogopen:

$("#dialog-form").dialog(
{
    open: function(event,ui)
    {
        // reset form
    }
});

You can also set it up later with : 您也可以稍后使用进行设置:

$("#dialog-form").on("dialogopen",function(event,ui)
{
     // reset form
});

But .dialog() widget should do it by default , so try to add 但是.dialog()小部件默认情况下应该这样做 ,因此请尝试添加

$("#dialog-form").dialog();`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM