Why is my condition onsubmit not returning false?
After asking a couple of questions I managed to create thi开发者_JAVA百科s form. I didn't have troubles making it return false if the submitted value was 0 but I cannot make it return false when it's the string 'Cambia de ciudad". I'm sure I'm messing with the quotes or something like that. This is the form:
<form role="search" method="get" action="http://chusmix.com/" onsubmit="if (document.getElementById('s')== 'Cambiá de ciudad") return false;'>
<input class="ubicacion" name="s" id="s" tabindex="1" onsubmit="if ((document.getElementById('s').value.length < 4) || (document.getElementById('s')== 'Cambiá de ciudad')) return false;" onfocus="if (this.value=='Cambiá de ciudad') this.value = ''" onblur="if(this.value == '') this.value = 'Cambiá de ciudad'" type="text" maxlength="80" size="28" value="Cambiá de ciudad">
<input type="submit" id="searchsubmit" value="Buscar" />
</form>
How do I make it work? Thanks
First, because you have got your quote marks muddled.
You open the attribute value with "
, and you open the string you are comparing the element to with '
. Then you go on to try to close that string with "
and the attribute value with '
.
Second, because you are trying to compare an element with a string, when you want to compare it's value.
The partially fixed code would look like:
onsubmit="if (document.getElementById('s').value == 'Cambiá de ciudad') return false;">
To be properly fixed, it would use unobtrusive JavaScript.
As an additional problem:
<input … onsubmit="…
… is nonsense. Only <form>
elements have onsubmit
attributes.
I'm not sure if it's the web prettifier or not, but your code looks like it's not using single and double quotes properly. Also, you need to not compare by element, but by text. Try this:
onsubmit="if (document.getElementById('s').value == 'Cambiá de ciudad') return false;"
精彩评论