admin管理员组

文章数量:1391943

I have a form which calculates a cost and sets the value of an input accordingly. For obvious reasons, I have used:

$('#totalcost').attr('disabled',true);

to prevent the user from being able to edit the cost.

However, when I do this, the PHP script I'm using to mail the form doesn't pick up the input (not just the value - it doesn't read the input at all). When the input is enabled, it works fine.

How can I prevent the user from editing the input while still having the PHP mailing the value? Or is there a better way to do this anyway?

I have a form which calculates a cost and sets the value of an input accordingly. For obvious reasons, I have used:

$('#totalcost').attr('disabled',true);

to prevent the user from being able to edit the cost.

However, when I do this, the PHP script I'm using to mail the form doesn't pick up the input (not just the value - it doesn't read the input at all). When the input is enabled, it works fine.

How can I prevent the user from editing the input while still having the PHP mailing the value? Or is there a better way to do this anyway?

Share Improve this question asked Nov 28, 2013 at 12:54 ElendilTheTallElendilTheTall 1,46218 silver badges26 bronze badges 3
  • The values of disabled form elements are not send – this is explicitly specified this way. Use readonly instead. – C3roe Commented Nov 28, 2013 at 12:56
  • Well better place an hidden field also , this might help stackoverflow./questions/6241943/… – Deepanshu Goyal Commented Nov 28, 2013 at 12:58
  • Then again, inputs can still be edited after pressing submit, an example tool would be the Firefox extension: tamperdata – Daryl Gill Commented Nov 28, 2013 at 12:59
Add a ment  | 

6 Answers 6

Reset to default 7

Make it readonly, not disabled:

$("#totalcost").attr('readonly', true);

You could also do it in the original HTML, since it's not something you really want to change back and forth dynamically.

<input id="totalcost" type="text" readonly>

Use Read Only property, Though i guess it wont work in internet exploer.

Add readonly attribute, like this:

$("#totalcost").attr('readonly', true);

Add property readonly="true".

$('#totalcost').attr('readonly',true);

you can try:

$("#totalcost").prop("readonly",true);

Use read only in html itself or in script

<input type="text" name="totalcost" id="totalcost" value="" readonly>

$('#totalcost').attr("readonly", true);

本文标签: javascriptMaking an input noneditable without disablingStack Overflow