设置“n”按钮的ID

时间:2015-11-22 12:30:05

标签: javascript

    //Display
        for (var z=0; z<n; z++) {   
            document.write("<tr><td>"+ z + "</td>");  // Serial
            document.write("<td>" + RandonValue + "</td>"); // Random Number
            document.write('<td><button onclick="reply_click()" id="button-' + z + '">Click me</button></td></tr>'); //onClick
        }

    //Set ID
    function reply_click(clicked_id){
        alert(clicked_id);

    }

不知何故,我为所有人获得了undefined

replay_click仅用于检查,我想在我有不同的ID时更改它。

3 个答案:

答案 0 :(得分:2)

您的z变量是这种情况下的计数器,您可以将其用于您的ID,请注意,您不能使用以数字开头的ID。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ExpressionEvaluatorLibrary;

namespace Unit_test
{
    class Program
    {
        static void Main(string[] args)
    {
       ConsoleKeyInfo cki = new ConsoleKeyInfo();


        Console.WriteLine("Allowed Operators are : +, -, *, /");

        do
        {
            string strUserInput = "";
            if (args.Length == 0)
            {
                Console.Write("\n\nEnter an Expression: ");
                strUserInput = Console.ReadLine();
            }
            else
            {
                // TO be able to run from command prompt.
                strUserInput = args[0];
            }

            IExpressionModel expressionModel = new ExpressionModel();

            string strValidate = expressionModel.ExpressionValidate(strUserInput);

            if (strValidate == "Valid")
            {
                try
                {
                    string strPostFixExpression = expressionModel.ConvertToPostfix(strUserInput);
                    // Console.WriteLine(strPostFixExpression);

                    string strResult = expressionModel.EvaluateExpression(strPostFixExpression);
                    Console.WriteLine("\n       The result is: " + strResult);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }                    
            }
            else
            {
                Console.WriteLine(strValidate);
            }

            Console.WriteLine("\nPress any key to continue; press the 'Esc' key to quit.");
            cki = Console.ReadKey(false);

        } while (cki.Key != ConsoleKey.Escape);
    }
 }
}


namespace ExpressionEvaluatorLibrary
{
  public interface IExpressionModel
  {
    string ExpressionValidate(string strUserEntry);
    string ConvertToPostfix(string strValidExpression);
    string EvaluateExpression(string strPostFixExpression);
  }
}


namespace ExpressionEvaluatorLibrary
{
  public class ExpressionModel : IExpressionModel
  {


    public string ExpressionValidate(string strUserEntry)
    {
        strUserEntry = strUserEntry.Trim();

        if (string.IsNullOrEmpty(strUserEntry))
            return "There was no entry.";

        if (strUserEntry.Length > 250) //250 seemed better than 254
            return "More than 250 characters entered.";

        string[] fixes = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

        bool boolStartsWith = fixes.Any(prefix => strUserEntry.StartsWith(prefix));
        if (!boolStartsWith)
            return "The expression needs to start with a number.";

        bool boolEndsWith = fixes.Any(postfix => strUserEntry.EndsWith(postfix));
        if (!boolEndsWith)
            return "The expression needs to end with a number.";

        if (!Regex.IsMatch(strUserEntry, "^[-0-9+*/ ]+$"))
            return "There were characters other than Numbers, +, -, * and /.";

        if (!Regex.IsMatch(strUserEntry, "[-+*/]"))
            return "Not a mathematical expression";

        string[] strOperator = Regex.Split(strUserEntry, @"\d+");
        for (int i = 1; i < strOperator.Length - 1; i++) //the first and last elements of the array are empty
        {
            if (strOperator[i].Trim().Length > 1)
                return "Expression cannot have operators together '" + strOperator[i] + "'.";
        }

        return "Valid";
    }



    public string ConvertToPostfix(string strValidExpression)
    {

        StringBuilder sbPostFix = new StringBuilder();
        Stack<Char> stkTemp = new Stack<char>();

        for (int i = 0; i < strValidExpression.Length; i++)
        {
            char chExp = strValidExpression[i];

            if (chExp == '+' || chExp == '-' || chExp == '*' || chExp == '/')
            {
                sbPostFix.Append(" ");

                if (stkTemp.Count <= 0)
                    stkTemp.Push(chExp);
                else if (stkTemp.Peek() == '*' || stkTemp.Peek() == '/')
                {
                    sbPostFix.Append(stkTemp.Pop()).Append(" ");
                    i--;
                }
                else if (chExp == '+' || chExp == '-')
                {
                    sbPostFix.Append(stkTemp.Pop()).Append(" ");
                    stkTemp.Push(chExp);
                }
                else
                {
                    stkTemp.Push(chExp);
                }
            }
            else
            {
                sbPostFix.Append(chExp);
            }
        }

        for (int j = 0; j <= stkTemp.Count; j++)
        {
            sbPostFix.Append(" ").Append(stkTemp.Pop());
        }

        string strPostFix = sbPostFix.ToString();
        strPostFix = Regex.Replace(strPostFix, @"[ ]{2,}", @" ");
        return strPostFix;
    }

    public string EvaluateExpression(string strPostFixExpression)
    {
        Stack<string> stkTemp = new Stack<string>();
        string strOpr = "";
        string strNumLeft = "";
        string strNumRight = "";

        List<String> lstPostFix = strPostFixExpression.Split(' ').ToList();

        for (int i = 0; i < lstPostFix.Count; i++)
        {
            stkTemp.Push(lstPostFix[i]);
            if (stkTemp.Count >= 3)
            {
                Func<string, bool> myFunc = (c => c == "+" || c == "-" || c == "*" || c == "/");
                bool isOperator = myFunc(stkTemp.Peek());

                if (isOperator)
                {
                    strOpr = stkTemp.Pop();
                    strNumRight = stkTemp.Pop();
                    strNumLeft = stkTemp.Pop();

                    double dblNumLeft, dblNumRight;
                    bool isNumLeft = double.TryParse(strNumLeft, out dblNumLeft);
                    bool isNumRight = double.TryParse(strNumRight, out dblNumRight);

                    if (isNumLeft && isNumRight)
                    {
                        double dblTempResult;
                        switch (strOpr)
                        {
                            case ("+"):
                                dblTempResult = dblNumLeft + dblNumRight;
                                stkTemp.Push(dblTempResult.ToString());
                                break;
                            case ("-"):
                                dblTempResult = dblNumLeft - dblNumRight;
                                stkTemp.Push(dblTempResult.ToString());
                                break;
                            case ("*"):
                                dblTempResult = dblNumLeft * dblNumRight;
                                stkTemp.Push(dblTempResult.ToString());
                                break;
                            case ("/"):
                                dblTempResult = dblNumLeft / dblNumRight;
                                stkTemp.Push(dblTempResult.ToString());
                                break;
                        }
                    }
                }
            }
        }

        return stkTemp.Pop();
     }
  }
}

答案 1 :(得分:0)

你可以像这样注入你的z变量,也可以加双引号(最佳实践:

document.write('<td><button onclick="somefunction(this)" id="mycell-' + z + '">Click me</button></td></tr>');

请注意,您最好将this传递给您的函数,因此在该函数中您可以引用该id:

function somefunction(elem) {
    alert('you clicked ' + elem.id);
}

答案 2 :(得分:0)

我为你创造了一个小提琴。核实。我希望它符合要求:)

https://jsbin.com/doxusi/edit?html,js,output

<table id="mytable"></table>

var html = '';
  var random = 5;
  for (var i = 1; i <= random; i++) {
    html += '<tr>' +
      '<td>' + i + '</td>' +
      '<td>Some Number</td>' +
      '<td><button id="id_' + i + '">Button ' + i + '</button></tr></td>';

  }

  document.getElementById('mytable').innerHTML = (html);