我试图根据两个ahref标签更改表单操作,一个是按钮,另一个是文本。当它单击按钮时,表单的操作应该是/profile.php?id=并且如果单击了文本,则该操作应该是/profile.php?uid= im卡在这个问题上。这是我在html中的代码:
<a href = "profile.php?/edit/employment-information/" id="edit"><input type="button" name="editemp" id="edit" value="Edit">
<a href="profile.php?/add/employment-information/" id="add">Add Information</a>
<form name="empform" method="post" action="profile.php" autofocus id="form">
<input name="employ" type="text" id="employ" pattern="[A-Za-z ]{3,20}" placeholder="Who is your employer?">
<input name="position" type="text" id="position" pattern="[A-Za-z ]{3,20}" placeholder="What is your job description?">
<input name="empadd" type="text" id="empadd" pattern="[A-Za-z0-9@#$% ,]{5,30}" placeholder="Where is your work address?">
<input name="empcont" type="text" id="empcont" pattern="[0-9]{11}" title="11-digit number" placeholder="Contact number">
答案 0 :(得分:2)
您可以使用
更改表单操作$("#form").attr("action", "your new action here");
你所做的就是把它放在click
处理程序中:
$("#elementID").click(function() {
//change form here
});
不确定代码上的哪些按钮应该改变这一点,所以我让它非常通用,你可以修改它。
答案 1 :(得分:0)
简单地向两个锚添加一个eventlistner,每个锚获得自己的代码来改变形式
$(document).ready(function(){
$('#edit').on('click', function(e){
e.preventDefault(); // Dont actually go to the location of the anchor
$('#form').attr('action', 'URL_IF_EDIT_IS_CLICKED'); // change it to this url
});
$('#add').on('click', function(e){
e.preventDefault(); // Dont actually go to the location of the anchor
$('#form').attr('action', 'URL_IF_ADD_IS_CLICKED'); // change it to this url
});
});
如果您有更多锚点,则以下方法可能更容易: 首先向锚点添加一个类(比如说'formActionChanger'),然后:
$(document).ready(function(){
// eventlistener on the anchors:
$('.formActionChanger').on('click', function(e){
e.preventDefault(); // Dont actually go to the location of the anchor
var newAction;
// start a switch to decide which new action is going to be used
switch(this.id){
case 'edit': newAction = 'URL_IF_EDIT_IS_CLICKED'; break;
case 'add': newAction = 'URL_IF_ADD_IS_CLICKED'; break;
default: newAction = 'DEFAULT_URL'; break;
}
$('#form').attr('action', newAction); // change it to this url
});
});