How do I add an onclick-handler to an input-element?
I need your help. This should be a simple-to-answer question. I want to modify a site to my needs and this is how far I got.
I have a formular
<form>
<div><select id="select1"><option>value x</option></select></div>
<div><select id="select2"><option>value x</option></select></div>
<div><input type="submit" name="submit" value="Submit changes"></div>
</form>
I want to check whether the value of the select-elements is equal or not. This should happen when I click the submit-button. If they are equal there should be a popup with text like: Are you sure this is right?
So I call the value of the selectfields with
function show_alert() {
var x = document.getElementById('select1').value;
var y = document.getElementById('select2').value;
//If the values are equal there should be an alert
if (x=y)开发者_StackOverflow中文版 {
alert ('Are you sure this is right?');
}
}
I guess that I should add an onClick-handler to the submit button like
<div><input type="submit" name="submit" value="Submit" onclick='show_alert()'></div>
How do I add this handler when I have no access to the site?
If there is anything wrong with my idea I'd be happy to hear your comment.
Thanks.
You could do something like this:
markup:
<form id="myform" onsubmit="show_alert(this);return false;">
<div><select id="select1"><option>value x</option></select></div>
<div><select id="select2"><option>value x</option></select></div>
<div><input type="submit" name="submit" value="Submit" /></div>
</form>
js:
function show_alert() {
var x = document.getElementById('select1').value;
var y = document.getElementById('select2').value;
if(x == y && confirm('Are you sure this is right?')){
document.myform.submit();
}
}
You can register an event handler using Javascript.
Note that you need to change your if
statement to if (x === y)
.
Writing if (x=y)
will assign x
to y
, then pass the value of y
to the if
check.
Note: you have an error in script. Is if (x==y) not x=y.
If I understood the question right you want to modify a file with no access to it?...
You don't need access to the site you need access to the file with the form. FTP access is most common.
If you don't have... you can't.
精彩评论