我有一个剧本:
<form id="myform">
<input type="text" value="" id="input1">
<input type="text" value="" id="input2">
<input type="submit" value="submit">
</form>
<img id="image" src="http://mydomain.com/empty.gif" />
<script>
$(document).ready(function () {
$("#myform").submit(function (ev) {
ev.preventDefault();
var val1 = $("#input1").val();
var val1 = $("#input2").val();
$("#image").attr("src", "http://mydomain.com/image?val1="+val1+"&val2="+val2);
});
});
</script>
如果用JavaScript编写它会是什么样子?
答案 0 :(得分:3)
<img id="image" src="http://mydomain.com/empty.gif" />
<script>
window.onload = function() { // Not all browsers support DOMContentLoaded
document.getElementById("myform").onsubmit = function() {
var val1 = document.getElementById("input1").value;
var val2 = document.getElementById("input2").value;
document.getElementById("image").src="http://mydomain.com/image?val1="+val1+"&val2="+val2;
return false;
};
};
</script>
如果您命名了可以使用的字段
window.onload = function() {
document.getElementById("myform").onsubmit = function() {
document.getElementById("image").src="http://mydomain.com/image?val1="+this.input1.value+"&val2="+this.input2.value;
return false;
};
};
您可能想要转义这两个值