English 中文(简体)
在我点击时,为什么我的纽子根本不工作?
原标题:Why is my button not working at all when I click it?

I am making a to-do list with JS , React and HTML. The button in the HTML code: <input type="button" value="Add your new task" id="newTaskBtn"> gets added with an event listener: document.getElementById( newTaskBtn ).addEventListener("click",addTask); which calls the function addTask():

function addTask(){
        if (document.getElementById( newTask ).value = " ") {
            alert( You have to write something. )
        } else {
            const root = ReactDOM.createRoot(document.getElementById( to-dos container ));
            root.render(<newTaskComponent content={document.getElementById( newTask ).value}/>)
            {() => setId(c = c + 1);}
        }
    }

which checks if the input box: <input type="text" name="addNewTask" id="newTask" value=" " placeholder="Add a new task"> is empty , if not , it calls the newTaskComponent component:

function newTaskComponent(props){
        return (
          <>
            <toDoId />
            <p>{props.content}</p>
            <Checkbox />
            <DeleteBtn />
          </>
        );
    }

which adds a checkbox, a delete button and gives the todo an id(Todo 1,Todo 2 ...) The toDoId() component:

function toDoId(){
        const [todos, setTodos] = useState([]);
        const addTodo = () => {
            setTodos((c) => [...c]);
          };

        return(
            <>
              <h1>{addTodo}</h1>
            </>
        )
    }

uses the setId() function declared at the addTask() function to give the todo an Id. Then the checkbox function:

function Checkbox(){
        if (document.getElementById( checkbox ).checked) {
            alert( Congratulations! )
        } 
        return (
            <>
            <input type= checkbox  name= checkbox  id= checkbox ></input>
            <label for= checkbox >Completed!</label>
              </>  
        )
    }

在任务完成后(检查箱时)向你表示祝贺。 删除纽芬兰语:

 function DeleteBtn() {
        return(
            <>
            <button onClick={newTaskComponent = " " && alert(Deleted)}>Delete</button>
            </>
        )
    }

它删除新的TaskComponent(基准部分),并提醒注意删除这项任务。

我认为,在催复国之后,将会发生争do,但似乎并非最糟糕的是,如果对空白投入进行检查的声明也不起作用,那就好像我没有在联合材料档案中写任何法典,所有来源代码都低于标准,我希望一位专家能够提供一些帮助(如果能够详细解释会更好)。

纽约总部

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="纽约总部"></script>
    </head>
    <body>
        <div id="container">
            <div id="to do list">
                <form>
                    <h1>To-Do List:</h1>
<input type="text" name="addNewTask" id="newTask" value=" " placeholder="Add a new task">
                    <div id="to-dos container">
                        
                    </div>
                    <input type="button" value="Add your new task" id="newTaskBtn">
                </form>
            </div>
        </div>
    </body>
</html>

纽约总部

import React, { useState } from  react ;
import ReactDOM from  react-dom/client ;

function myFunction(){
    document.getElementsByTagName( body ).addEventListener("load",myFunction);
    document.getElementById( newTaskBtn ).addEventListener("click",addTask);

    function DeleteBtn() {
        return(
            <>
            <button onClick={newTaskComponent = " " && alert(Deleted)}>Delete</button>
            </>
        )
    }

    function Checkbox(){
        if (document.getElementById( checkbox ).checked) {
            alert( Congratulations! )
        } 
        return (
            <>
            <input type= checkbox  name= checkbox  id= checkbox ></input>
            <label for= checkbox >Completed!</label>
              </>  
        )
    }

    function toDoId(){
        const [todos, setTodos] = useState([]);
        const addTodo = () => {
            setTodos((c) => [...c]);
          };

        return(
            <>
              <h1>{addTodo}</h1>
            </>
        )
    }

    function newTaskComponent(props){
        return (
          <>
            <toDoId />
            <p>{props.content}</p>
            <Checkbox />
            <DeleteBtn />
          </>
        );
    }

    function addTask(){
        if (document.getElementById( newTask ).value = " ") {
            alert( You have to write something. )
        } else {
            const root = ReactDOM.createRoot(document.getElementById( to-dos container ));
            root.render(<newTaskComponent content={document.getElementById( newTask ).value}/>)
            {() => setId(c = c + 1);}
        }
    }

}

请帮助,如果问题偏离了排位规则,请不要删除问题,请感谢你。


Edit:我在审查冲积和山角后,改变了源代码,但是,在紧紧靠ton子之后,它仍会装满负荷,但我希望它会有所帮助。

纽约总部

<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="纽约总部"></script>
    </head>
    <body id="body" onload="myFunction()">
        <div id="container">
            <div id="to do list">
                <form>
                    <label for="addNewTask">To-Do List:</h1><br>
                    <input type="text" name="addNewTask" id="newTask" value="" placeholder="Add a new task"><br>
                    <div id="to-dos container"></div><br>
                    <input type="button" value="Add a new To-Do" id="addNewTask" onclick="addTask()">
                </form>
            </div>
        </div>
    </body>
</html>

纽约总部

import React from  react ;
import ReactDOM from  react-dom/client ;

let input = document.getElementById("newTask").value ;

function myFunction(){
    function addTask(){
        if(input ===   ||input ===    ){
           alert( You have to write something )
        }else{
            const root = ReactDOM.createRoot(document.getElementById( to-dos container ))
            root.render(<NewTaskComponent />)
        }
    }
    function NewTaskComponent(){
        return(
            <div>
                <ToDoId />
                <p>{`${input}`}</p>
                <Checkbox />
                <Delete />
            </div>
        )  
    }
    function ToDoId(){
        let c = 1
    const add = (function(){
        return function () {c += 1; return c}
    })();
    add();
}

    function Checkbox(){
        return(
            <input type= checkbox  id= checkbox  onChange= {check()}></input>
        )
    }
    function check(){
        if(document.getElementById( checkbox ) === true){
           alert( Congrats! )
        }else{
            alert( Remember to complete your tasks! )
        }
    }
    function Delete(){
        return(
            <input type= button  onClick= {erase()}></input>
        )
    }
    function erase(){
        del();
        decrement();
    }
    function del(){
        const container = document.getElementById( to-dos container )
        const root = ReactDOM.createRoot(container);
        root.render(container);
        root.unmount();
    }

    const decrement = (function(){
        return function () {c -= 1; return c}
    })();
}

专 员 感谢Jacob!

问题回答

The first place to start will be looking into why the if statement which checks for blank input also does not work .
The process will never start because your check
if (document.getElementById( newTask ).value = " ") { is using a single =, which will is assigning instead of checking.
Try changing this to == or === and see if you get much further.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签