Button doesn't work in Chrome

ε祈祈猫儿з 提交于 2019-12-11 08:24:09

问题


I have

<button name="foo" value="bar" onclick="submit()" >foobar</button>

When I click the button in firefox, the value is transferred (is foo) and I can read the value like the following:

dim mode                : mode                = lcase(request("foo"))  

But the value is empty when I perform the same action in Chrome.

Could anyone help?


回答1:


Different browsers have different default submit behaviors for buttons. If you want a button to submit the form (and thus post its name and value pair), you'll have to use:

<button type="submit" name="foo" value="bar" >foobar</button>

The type attribute for button can be submit, button, or reset and unless this is explicitly specified, will vary from browser to browser.




回答2:


You have a quote missing. It should look like this:

<button name="foo" value="bar" onclick="submit()" >foobar</button>

In addition, submit() is a method on a form, so you must make sure your button is within <form></form> tags.

From the W3C specification:

Important: If you use the element in an HTML form, different browsers may submit different values. Internet Explorer, prior version 9, will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the <input> element to create buttons in an HTML form.




回答3:


You need to quote your attributes:

<button name="foo" value="bar" onclick="submit()" >foobar</button>

Also "submit" is a reserved word in JavaScript. If you have a function called "submit" it may not work.

List of reserved words: http://www.javascripter.net/faq/reserved.htm




回答4:


If you will it makes easier for google or other to find your form. Give the button an title and alt:

 <button name="foo" title="submit" alt="submit for contact" valeu="" onclick="submit()" >foobar</button>



回答5:


From what I remember, old versions of IE (maybe even 8) treat only the good old <input type="submit" /> as "real" submit button that is part of the form thus sending its value. So <button> in such browsers won't send its value.

Change the HTML to this and it should work for all browsers:

<input type="submit" name="foo" value="foobar" onclick="this.value = 'bar';" />

As the value sent is the value seen, I had to use JavaScript trick to change the value upon click - as far as I know, can't do that without using client side script.



来源:https://stackoverflow.com/questions/9314706/button-doesnt-work-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!