开发者

Jquery : Disabled 'input' field not available in form

开发者 https://www.devze.com 2023-02-21 02:00 出处:网络
I\'m trying to dynamically set a an input value(\"choice\" in my case) inside a form based on user inputs. After setting the desired value, I want to disable the field so that user cannot change it. H

I'm trying to dynamically set a an input value("choice" in my case) inside a form based on user inputs. After setting the desired value, I want to disable the field so that user cannot change it. Here is the code i'm using.

// HTML
<select name="0-choices" id="id_0-choices">
<option value="Phone">Phone</option>
<option value="Fax">Fax</option>
<option value="Voicemail">Voicemail</option>
</select>

//Javascript
$(cfield).children().first().removeAttr('selected');
$(cfield).children('option[value="Voicemail"]').attr('selected','selected');
//$(cfield).val('Voicemail');
$(cfield).attr('disabled','disabled');

The problem is, if I disable the field, the field is not available in the posted data. So my validation function complains of data no开发者_运维知识库t available. How can I "freeze" the input field and yet be able to post it.


I got to this problem too, and guess you try to disable the fields while sending Ajax request, the best solution i found was using readonly instead:

$(cfield).attr('readonly', true);

Users can't modify the field's value but they are still read


You can disable the field, and then copy its value into a hidden form field.

HTML

<input type="hidden" name="cfield_hidden"/>

JavaScript

// snip...
$(cfield).attr('disabled','disabled');
$('#cfield_hidden').val($(cfield.val());

Or, you could enable the options immediately before submitting the form:

$('some-form-selector').submit(function ()
{
    $(this).find('option').attr('disabled', false);
});


make a hidden input field like

$('.your-form').append("<input type='hidden' name='0-choices' value='"+$('cfield option:selected).val()+"' />")

as disabled inputs won't be submitted ..


You can use a hidden field to store the value you whant to send once you disable the input field.

Something like this:

$(cfield).append('<input type="hidden" id="hiddenField" value="'+$(cfield).val()+'"/>');
$(cfield).attr('disabled','disabled');


as you say disabled inputs are not included in the post data, your best option is to either fix your server side logic or to store a input hidden field with name 0-choices and the value you want posted.


i would clone the field on submit and set remove the disabled attr. And append it to your form as display none.

Or simply remove the disabled onsubmit.

PS: your username would be cooler with a M instead of the N ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号