我正在尝试根据populate values
中选定的项目从数据库中textbox
到combo box
,它起作用了,但是每当我选择该值并填充textbox
时,它将没有submitting
表单值的表单值插入数据库。请任何人帮我。
这是我的下面的代码:
<script type="text/javascript">
function reset(){
alert("Yeah!");
document.getElementByID("subzone").value = "";
}
</script>
<script type="text/javascript">
window.history.forward();
function noback() { window.history.forward(); }
</script>
<script type="text/javascript">
function myoriginaldata() {
document.getElementById("gn").value = document.getElementById("hgn").value;
document.getElementById("mn").value = document.getElementById("hmn").value;
document.getElementById("ag").value = document.getElementById("hag").value;
}
</script>
<body>
<form action="/FeezyMusic/B_book_artist" method="post" class="form-horizontal row-fluid">
<div class="control-group">
<label class="control-label" for="basicinput">Select Artist to book here...</label>
<div class="controls">
<select tabindex="1" name="group_name" id="gid" onchange="this.form.submit()" data-placeholder="Select here.." class="span8">
<option>Select</option>
<%
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/feezymusic","root","");
sqlQuery = "Select * from bookartist";
st = conn.createStatement();
rst = st.executeQuery(sqlQuery);
while (rst.next()) {
%>
<option><%=rst.getString("ARTISTNAME")%></option>
<%
}
rst.close();
st.close();
conn.close();
%>
</select>
</div>
</div>
<%
groupname_var=request.getParameter("group_name");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/feezymusic","root","");
sqlQuery="select ArtistGenre,ArtistManager,Agent from bookartist where ArtistName='"+groupname_var+"'";
st=conn.createStatement();
rst=st.executeQuery(sqlQuery);
while(rst.next())
{
data_genre=rst.getString("ArtistGenre");
data_manager = rst.getString("ArtistManager");
data_agent = rst.getString("Agent");
}
rst.close();
st.close();
conn.close();
%>
<input type="hidden" id="hgroupname_id" value="<%=groupname_var%>">
<div class="control-group">
<label class="control-label" for="basicinput">Artist Genre</label>
<div class="controls">
<input type="text" id="basicinput" name="genre" value="<%=data_genre%>" placeholder="Artist..." class="span8" disabled>
</div>
</div>
<div class="control-group">
<label class="control-label" for="basicinput">Artist Manager</label>
<div class="controls">
<input type="text" name="manager" value="<%=data_manager%>" id="basicinput" placeholder="Manager..." class="span8" disabled>
</div>
</div>
<div class="control-group">
<label class="control-label" for="basicinput">Feezy Music Agent</label>
<div class="controls">
<input type="text" name="agent" value="<%=data_agent%>" id="basicinput" placeholder="Agent..." class="span8" disabled>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" value="Submit">
</div>
</div>
<%
String genre_var = request.getParameter("genre");
String manager_var = request.getParameter("manager");
String agent_var = request.getParameter("agent");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/feezymusic","root","");
st=conn.createStatement();
%>
<input type="hidden" id="hgn" value="<%=genre_var%>">
<input type="hidden" id="hmn" value="<%=manager_var%>">
<input type="hidden" id="hag" value="<%=agent_var%>">
<%
if(groupname_var!=null && genre_var!=null && manager_var!=null && agent_var!=null){
if(!groupname_var.equals("") && !genre_var.equals("") && !manager_var.equals("") && !agent_var.equals("")){
%>
<script type="text/javascript">
myoriginaldata();
</script>
<%
st.executeUpdate("update bookartist set ArtistGenre='"+genre_var+"',ArtistManager='"+manager_var+"',Agent='"+agent_var+"' where ArtistName='"+groupname_var+"'");
}
}
st.close();
conn.close();
%>
<script type="text/javascript">
document.getElementById("gid").value = document.getElementById("hgroupname_id").value;
</script>
</form>
答案 0 :(得分:0)
在您的代码中,您将onchange="this.form.submit()"
赋予了select box
,因此,每更改select
的值形式,都会得到 submitted :默认。为避免这种情况,只需如下更改代码:
<select tabindex="1" name="group_name" id="gid"
onchange="myfunction(this.value);" data-placeholder="Select here.." class="span8">
<!--^added this function and passing value to it-->
..
</select>
现在您的<script></script>
中的操作如下:
<script type="text/javascript">
function myfunction(values)
{
document.getElementById("yourtextboxid").value=values; //<--assigning value of select to textbox
}
</script>
更新1:
现在要从数据库中获取选定的值,可以使用ajax
。因为在这里您不想提交form
。现在您的script
如下图所示:>
<script type="text/javascript">
function myfunction(values) {
document.getElementById("yourtextboxid").value=values; //<--assigning value of select to textbox
//ajax call
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
//do something
alert("success");
document.getElementById("yourdivid").innerHTML = this.responseText;
}
};
//sending group_name on jsp page
request.open("GET", "yourjsppage?group_name="+values, true);
request.send();
}
</script>
还要添加一个null
值的检查,即
<%
if(request.getParameter("group_name") ! = null ){
//your db code
groupname_var=request.getParameter("group_name");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/feezymusic","root","");
sqlQuery="select ArtistGenre,ArtistManager,Agent from bookartist where ArtistName='"+groupname_var+"'";
st=conn.createStatement();
rst=st.executeQuery(sqlQuery);
while(rst.next())
{
String data_genre=rst.getString("ArtistGenre");
String data_manager = rst.getString("ArtistManager");
String data_agent = rst.getString("Agent");
}
rst.close();
st.close();
conn.close();
//whatever will be in out.println() will be sent back as response
out.println("<input type='hidden' id='hgroupname_id' value='"+groupname_var+"'>
<div class='control-group'>
<label class='control-label' for='basicinput'>Artist Genre</label>
<div class='controls'>
<input type='text' id='basicinput' name='genre' value='"+data_genre+"' placeholder='Artist...' class='span8' disabled>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='basicinput'>Artist Manager</label>
<div class='controls'>
<input type='text' name='manager' value='"+data_manager+"' id='basicinput' placeholder='Manager...' class='span8' disabled>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='basicinput'>Feezy Music Agent</label>
<div class='controls'>
<input type='text' name='agent' value='"+data_agent+"' id='basicinput' placeholder='Agent...' class='span8' disabled>
</div>
</div>");
}
%>
此外,在您当前的jsp页面中添加<div></div>
,将其中的数据替换为ajax
的响应。即:
<!--adding div with id to jsp page-->
<div id="yourdivid">
<input type="hidden" id="hgroupname_id" value="<%=groupname_var%>">
<div class="control-group">
<label class="control-label" for="basicinput">Artist Genre</label>
<div class="controls">
<input type="text" id="basicinput" name="genre" value="<%=data_genre%>" placeholder="Artist..." class="span8" disabled>
</div>
</div>
..
.....
<div class="controls">
<input type="text" name="agent" value="<%=data_agent%>" id="basicinput" placeholder="Agent..." class="span8" disabled>
</div>
</div>
</div>
<!--closing div-->