2 jerome66 jerome66 于 2015.07.27 06:37 提问

js怎么根据点击的按钮不同,在不同的文本框中显示内容(jsp中写条件语句)

三个文本框的id分别为"i_rescive,"i_cc",i_bcc,怎么在jsp中根据id判断应该显示在哪个文本框中,就是条件语句怎么写

 window.opener.document.getElementById("i_rescive").value += tVal;

//显示联系人列表
//function showSendPage() {
function openNewWindow(){
window.open("<%=basePath%>user/showUserList.action",'linkman',"width=260 ,height=345,top=200,left="+((window.screen.width/2)-200));
}

function openNewccWindow(){
    window.open("<%=basePath%>user/showUserList.action",'linkman',"width=260 ,height=345,top=200,left="+((window.screen.width/2)-200));
}   

function openNewbccWindow(){
    window.open("<%=basePath%>user/showUserList.action",'linkman',"width=260 ,height=345,top=200,left="+((window.screen.width/2)-200));
}   
 <tr>
    <td class="label">发件人:</td>
    <s:set var="alias" value="user.true"></s:set>
    <%String name = ""; %>
    <td><span class="person">
        <s:if test='#alias!=null&&#alias!=""'>${alias}<%name="\""+pageContext.getAttribute("alias")+"\"&lt;"+user.getName()+"@ljg.com&gt;";%></s:if>
            <s:else>"${user.name}"<%name="\""+user.getName()+"\"&lt;"+user.getName()+"@ljg.com&gt;";%></s:else>&lt;${user.name}@ljg.com&gt;
        </span>
        <input type="hidden" name="mailInfoFB.from" value='<%=name %>' id="i_from"/>
    </td>
    <td align="right"><a href="javascript:bb()" id="acc">添加抄送</a>&nbsp;|&nbsp;<a href="javascript:cbb()" id="abcc">添加密送</a></td>
</tr>
<tr>
    <td class="label">收件人:</td>
    <td class="e_input" colspan="2"><textarea  title="多个收件人请以分号(;)分隔"  class="s_input" id="i_rescive" onfocus="inputfocus()"  name="mailInfoFB.to"><s:property value="mailInfoFB.to"/></textarea>
    <a href="javascript:;" id="tianjia" onclick="openNewWindow();">添加</a>
    </td>
</tr>
<tr id="cc" style="display: none;">
    <td class="label">抄送:</td>
    <td class="e_input" colspan="2"><textarea  title="多个抄送人请以分号(;)分隔"  class="s_input" id="i_cc" onfocus="inputfocus()"  name="mailInfoFB.cc"><s:property value="mailInfoFB.cc"/></textarea>
    <a href="javascript:;" id="addcc" onclick="openNewccWindow();">添加</a>
    </td>
</tr>
<tr id="bcc" style="display: none;">
    <td class="label">密送:</td>
    <td class="e_input" colspan="2"><textarea   title="多个密送人请以分号(;)分隔"  class="s_input" id="i_bcc" onfocus="inputfocus()"  name="mailInfoFB.bcc"><s:property value="mailInfoFB.bcc"/></textarea>
    <a href="javascript:;" id="addbcc" onclick="openNewbccWindow();">添加</a>
    </td>
</tr>

以下是新窗口jsp中的内容

$().ready(function(){
$("#selectUserBtn").click(function(){
var data = [];
$("[name='cbUserName']:checked").each(function(){
data.push($(this).attr("value")+"@ljg.com");
});
var len = data.length;
if(len > 0){
var dataVal = '';
for(var i = 0;i <len;i++){
var tmp = data[i];
if(i<=len-1){
tmp += ";";
}
dataVal+=tmp;
}
var tVal = dataVal;
window.opener.document.getElementById("i_rescive").value += tVal;
window.close();
}else{
alert("请选择联系人!");
}
});


5个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.27 07:07
已采纳
 <!DOCTYPE html>
<html>
<script>
function btnClick(self){
  if(self.id=='btn_rescive'){//正规的if判断
     document.getElementById('i_rescive').value='1';
  }else{//两种不同的条件运用方式,根据后缀一样来获取input的id
     var str = self.id.replace('btn_','i_');
     document.getElementById(str).value=str;
  }
}
</script>
<body>
<div>
<input type='text' id='i_rescive'/>
<input type='text' id='i_cc'/>
<input type='text' id='i_bcc'/>
</div>
<div>
<input type='button' onclick="btnClick(this)" value='1' id='btn_rescive'/>
<input type='button' onclick="btnClick(this)" value='2' id='btn_cc'/>
<input type='button' onclick="btnClick(this)" value='3' id='btn_bcc'/>
</div>
</body>
</html>

danielinbiti
danielinbiti 回复jerome66: http://www.cnblogs.com/LuckCoffey/articles/1867236.html可以直接通过函数交互
2 年多之前 回复
jerome66
jerome66 请问一下,我这个是要打开一个新窗口,在新窗口的jsp中选择,然后把选择的字符串传给输入框,请问在新打开的jsp中怎么判断是哪个按钮打开的
2 年多之前 回复
humeSnail
humeSnail   2015.07.27 07:55

点击的文本可以用this来表示

yuke198907
yuke198907   2015.07.27 08:19

或者你点击不同的按钮,执行不同的js函数呀

showbo
showbo   Ds   Rxr 2015.07.27 09:50

打开窗体的时候传递控件的id值,在被打开也获取就行

 <script>

    function openNewccWindow(id) {
        window.open("<%=basePath%>user/showUserList.action#"+id, 'linkman', "width=260 ,height=345,top=200,left=" + ((window.screen.width / 2) - 200));
    }

    function openNewbccWindow(id) {
        window.open("<%=basePath%>user/showUserList.action#" + id, 'linkman', "width=260 ,height=345,top=200,left=" + ((window.screen.width / 2) - 200));
    }
</script>
<tr>
    <td class="label">发件人:</td>
    <s:set var="alias" value="user.true"></s:set>
    <%String name = ""; %>
    <td>
        <span class="person">
            <s:if test='#alias!=null&&#alias!=""'>${alias}<%name="\""+pageContext.getAttribute("alias")+"\"&lt;"+user.getName()+"@ljg.com&gt;";%></s:if>
            <s:else>"${user.name}"<%name="\""+user.getName()+"\"&lt;"+user.getName()+"@ljg.com&gt;";%></s:else>&lt;${user.name}@ljg.com&gt;
        </span>
        <input type="hidden" name="mailInfoFB.from" value='<%=name %>' id="i_from" />
    </td>
    <td align="right"><a href="javascript:bb()" id="acc">添加抄送</a>&nbsp;|&nbsp;<a href="javascript:cbb()" id="abcc">添加密送</a></td>
</tr>
<tr>
    <td class="label">收件人:</td>
    <td class="e_input" colspan="2">
        <textarea title="多个收件人请以分号(;)分隔" class="s_input" id="i_rescive" onfocus="inputfocus()" name="mailInfoFB.to"><s:property value="mailInfoFB.to" /></textarea>
        <a href="javascript:;" id="tianjia" onclick="openNewWindow('i_rescive');">添加</a>
    </td>
</tr>
<tr id="cc" style="display: none;">
    <td class="label">抄送:</td>
    <td class="e_input" colspan="2">
        <textarea title="多个抄送人请以分号(;)分隔" class="s_input" id="i_cc" onfocus="inputfocus()" name="mailInfoFB.cc"><s:property value="mailInfoFB.cc" /></textarea>
        <a href="javascript:;" id="addcc" onclick="openNewccWindow('i_cc');">添加</a>
    </td>
</tr>
<tr id="bcc" style="display: none;">
    <td class="label">密送:</td>
    <td class="e_input" colspan="2">
        <textarea title="多个密送人请以分号(;)分隔" class="s_input" id="i_bcc" onfocus="inputfocus()" name="mailInfoFB.bcc"><s:property value="mailInfoFB.bcc" /></textarea>
        <a href="javascript:;" id="addbcc" onclick="openNewbccWindow('i_bcc');">添加</a>
    </td>
</tr>

showUserList.action

  window.opener.document.getElementById(location.hash.substring(1)).value += tVal;
sina_2831808769
sina_2831808769   Rxr 2015.07.27 22:30
Csdn user default icon
上传中...
上传图片
插入图片