简体   繁体   English

使用JavaScript / jQuery决定单击文本时要使用哪个超链接

[英]Using JavaScript/jQuery to decide which hyperlink to use when text is clicked

I am still new to javascript and am not sure about the syntax or if my thinking is correct. 我对javascript还是陌生的,不确定语法或我的想法是否正确。 If anyone can provide me advice on how to do this that would be great. 如果有人可以为我提供有关如何执行此操作的建议,那将很好。 Here is what I need to do I have a form where a user can either provide there own credit memo number or the system generates one on its own so I will have two seperate variables. 这是我需要做的,我有一个表格,用户可以在该表格中提供自己的贷项凭证编号,或者系统自己生成一个贷项凭证编号,因此我将有两个单独的变量。

What I want is that when they click on a link called Create Credit Memo the one of the variables will be sent within the link. 我想要的是,当他们单击名为Create Credit Memo的链接时,变量之一将在链接内发送。 If they provide their own number then that number should be sent otherwise the automatic number should be sent. 如果他们提供自己的号码,则应发送该号码,否则应发送自动号码。 After much time looking through the web this is what I thought would work but I think I am messing up the syntax or doing it completely wrong. 经过很多时间浏览网络后,我认为这是可行的,但是我认为我搞砸了语法或完全错误了。

Jave Script - I am sure my syntax is incorrect Jave Script-我确定我的语法不正确

$('#creditmemolink').click(function(){ 
{

if (&('#creditmemo').is(':defined'))
{   
    var cmnum = document.getElementById('creditmemo').value;
    var ttUrl = "somepage.cfm?creditmemos=#cmnum#&claimid=9988";
    window.open(ttUrl,"_blank");
} else {
    var cmnum = document.getElementById('cmnum').value;
    var ttUrl = "somepage.cfm?creditmemos=#cmnum#&claimid=9988";
    window.open(ttUrl,"_blank");
    return false;
}
}

Here is the HTML within the body 这是正文中的HTML

<div id="DspTxt" style="display:none;" class="txtmedb"> 
 Own Credit Memo: <input type="text" size="25" name="creditmemo" id="creditmemo"   
 class="txtsv" onChange="CreditMemoText()">&nbsp;&nbsp;&nbsp;

                                        <cfset cmnum = #htmlEditFormat(GETCLAIM.PO_NBR)# & #TimeFormat(NOW(),"hhmm")#>
<cfset cmnum = #cmnum# & #htmlEditFormat(url.claimid)#>
<cfset cmnum = #cmnum# & 'CM'>

      <a href="#" id="credmemolink">Create Credit Memo Test</a>

</div>

Any advice on how you would do this would be greatly appreicated. 关于如何执行此操作的任何建议将不胜感激。 Has stated I am still new to both coldfusion and Java so still learning the best way and correct syntax to use. 已经说过,我对Coldfusion和Java还是陌生的,所以仍然学习最佳方法和正确的语法。

Thanks in advance for your assistances on this. 在此先感谢您的协助。

try the below code 试试下面的代码

$('#creditmemolink').click(function(){ 
{

if ($('#creditmemo'))
{   
    var num = $('#creditmemo').val();
    //checking if user entered any value
    if(num) {

    } else {
       //executed when user did not provide any input
    }
}});

I'm not exactly sure what your ColdFusion is doing, but I can help with the JavaScript part: 我不确定您的ColdFusion在做什么,但是我可以在JavaScript部分提供帮助:

I think what you want to do to your markup is something like: 我认为您想对标记做些什么:

<form id="myForm" action="somepage.cfm" method="GET">
  <input type="hidden" name="claimid" value="9988">
  <input type="hidden" name="creditmemos" id="cmnum">
  <label>
    Own Credit Memo: <input type="text" name="creditmemo" id="creditmemo">
  </label>
  <a href="#" id="credmemolink" data-cmnum="numfromcf">Create Credit Memo Test</a>
</form>

And then for your JS: 然后为您的JS:

$("#credmemolink").on("click", function(e) {
  var $myForm = $("#myForm"),
      cmnum;

  e.preventDefault();

  if ($("#creditmemo").val() !== "") {
    cmnum = $("#creditmemo").val();
  } else {
    cmnum = $("#credmemolink").data("cmnum");
  }

  $("#cmnum").val(cmnum);
  $myForm.submit();
});

This will prevent the default action on the link, then check the value of the field to see if they entered anything. 这将阻止对链接执行默认操作,然后检查字段的值以查看是否输入了任何内容。

It will then set a hidden field with the value you need and then submit your form. 然后,它将使用所需的值设置一个隐藏字段,然后提交表单。

Your page will be easier to code and use if you don't combine forms and anchor tags. 如果您不将表单和锚标记结合在一起,则页面将更易于编码和使用。 Specifically, you don't need an anchor tag and you don't need javascript. 具体来说,您不需要锚标记,也不需要javascript。 All you need is a form with a textbox and submit button. 您只需要一个带有文本框和提交按钮的表单。 Then you can process it like this. 然后,您可以像这样处理它。

<cfscript>
if (len(trim(form.creditmemo))) {
claimId = form.creditmemo;
}
else {
code to generate one
}
etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM